<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="fromData.id?'编辑部门':'新增部门'" :visible="dialogShow" @close="addCancel">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="fromData" label-width="120px" :model="fromData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="fromData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="fromData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="fromData.manager" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in staffEasyList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="fromData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click.native="addConfirm">确定</el-button>
        <el-button size="small" @click.native="addCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getTissueList, addDepartments, getDepartmentsDetails, editDepartmentsDetails } from '@/api/departments'
import { getStaffEasy } from '@/api/employees'

export default {
  props: {
    dialogShow: {
      type: Boolean,
      required: true
    },
    // 从树形子组件给父组件在传过来的当前点击的数据
    curveData: {
      type: Object,
      required: true
    }
  },
  data() {
    // name的重复验证
    const inspectName = async(rule, value, callback) => {
      let judge = false
      const depts = (await getTissueList()).depts

      if (this.fromData.id) {
        judge = depts.filter(v => v.pid === this.curveData.pid).some(v => value === v.name && v.id !== this.curveData.id)
      } else judge = depts.filter(v => v.pid === this.curveData.id).some(v => value === v.name)
      if (judge) {
        callback(new Error('部门名称重复'))
      } else callback()
    }
    // code的重复验证
    const inspectCode = async(rule, value, callback) => {
      let judge = false
      const depts = (await getTissueList()).depts
      if (this.fromData.id) judge = depts.some(v => value === v.code && v.id !== this.curveData.id)
      else judge = depts.some(v => value === v.code)
      if (judge) {
        callback(new Error('部门编码重复'))
      } else callback()
    }
    return {
      // 表单数据
      fromData: {
        code: '',
        introduce: '',
        manager: '欧阳锋',
        name: ''
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' },
          { validator: inspectName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' },
          { validator: inspectCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请选择部门负责人', trigger: 'blur' },
          { max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { max: 300, message: '长度在 1 到 300 个字符', trigger: 'blur' }
        ]
      },
      // 员工简单信息
      staffEasyList: []
    }
  },
  async mounted() {
    // 钩子函数
    // 获取员工简单信息
    this.staffEasyList = await getStaffEasy()
    // console.log(this.staffEasyList)
  },
  methods: {
    // 根据ID查询部门详情请求
    async getDepartmentsDetails(id) {
      this.fromData = await getDepartmentsDetails(id)
    },
    // 点击取消
    addCancel() {
      this.$emit('addCancel')
      // 重置表单对象
      this.fromData = {
        code: '',
        introduce: '',
        manager: '欧阳锋',
        name: ''
      }
      // 重置表单验证
      this.$refs.fromData.resetFields()
    },
    // 点击确定
    async addConfirm() {
      await this.$refs.fromData.validate()
      if (this.fromData.id) {
        // 发送编辑请求
        await editDepartmentsDetails(this.fromData)
        this.$message.success('编辑成功')
      } else {
        // 发送新增请求
        await addDepartments({ ...this.fromData, pid: this.curveData.id })
        this.$message.success('新增成功')
      }
      this.$emit('addConfirm')
    }
  }
}
</script>

<style lang='less' scoped>
</style>
